package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;
import com.tang.user.UserInfo;
import com.tang.user.UserInfoManager;
import com.tang.util.GsonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.bean.CoordinateBean;
import top.hidoctor.auscultation.bean.DictionaryBean;
import top.hidoctor.auscultation.bean.MarkDetailBean;
import top.hidoctor.auscultation.bean.PatientLocalInfo;
import top.hidoctor.auscultation.bean.ProblemBean;
import top.hidoctor.auscultation.bean.SPBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.WorkbenchContract;
import top.hidoctor.auscultation.net.req.DictionaryReq;
import top.hidoctor.auscultation.net.req.MarkReq;
import top.hidoctor.auscultation.widget.mark.bean.MarkBean;

public class WorkbenchPresenter extends PresenterWrapper<WorkbenchContract.View> implements WorkbenchContract.Presenter {


    public WorkbenchPresenter(Context context, WorkbenchContract.View view) {
        super(context, view);
    }




    @Override
    public void getMarkDetail(String labelId, String SampleId) {
        mView.showLoading();
        Map<String, Object> map = new HashMap<>();
        map.put("operator", UserInfoManager.getInstance().getUserInfo().getUserId());
        add(mService.getMarkDetail(labelId, SampleId, map)
                .compose(this.<MarkDetailBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<MarkDetailBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<MarkDetailBean> response) {
                        mView.hideLoading();
                        mView.getMarkDetailSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void downloadFile(String fileId) {
        mView.showLoading();
        Map<String, Object> map = new HashMap<>();
        map.put("path", fileId);
        add(mService.downloadFile("2", map)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = (String) response.getData();
                        SPBean spBean = GsonUtil.parseJson(data, SPBean.class);
                        mView.downloadFileSuc(spBean);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

//    @Override
//    public void downloadFile2(String fileId, int yinliang) {
//        mView.showLoading();
//        Map<String, Object> map = new HashMap<>();
//        map.put("path", fileId);
//        map.put("type", yinliang);
//        add(mService.downloadFile("1", map)
//                .compose(this.getTransformer())
//                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
//                    @Override
//                    public void onSuccess(BaseResponse<Object> response) {
//                        mView.hideLoading();
//                        String data = (String) response.getData();
//                        SPBean spBean = GsonUtil.parseJson(data, SPBean.class);
//                        mView.downloadFileSuc(spBean);
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        throwable.printStackTrace();
//                    }
//                }));
//    }

    @Override
    public void getDictionaryList1(final DictionaryReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.dictionaryList(maps)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = GsonUtil.toJsonStr(((LinkedTreeMap) response.getData()).get("result"));
                        if (!TextUtils.isEmpty(data)) {
                            List<ProblemBean> problemBeans = GsonUtil.parseJson(data, new TypeToken<List<ProblemBean>>() {
                            });
                            mView.getDictionaryListSuc1(problemBeans);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void getDictionaryList2(final DictionaryReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        add(mService.dictionaryList(maps)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        String data = GsonUtil.toJsonStr(((LinkedTreeMap) response.getData()).get("result"));
                        if (!TextUtils.isEmpty(data)) {
                            List<ProblemBean> problemBeans = GsonUtil.parseJson(data, new TypeToken<List<ProblemBean>>() {
                            });
                            mView.getDictionaryListSuc2(problemBeans);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void updataMark(final String labelId, final String sampleId, String markId, MarkReq req) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils(req).getMaps();
        PatchRequest request = new PatchRequest(maps);
        add(mService.updataMark(labelId, sampleId, markId, request.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        getMarkDetail(labelId, sampleId);
                        mView.updataMarkSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void updataMarks(String labelId, String sampleId, String userId) {
        mView.showLoading();
        Map<String, Object> maps = new NetMapEncryptUtils("").getMaps();
        PatchRequest request = new PatchRequest(maps);
        add(mService.updataMarks(labelId, userId, sampleId, request.getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updataMarksSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

}
